package com.example.myapplication;

import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class NewsDetailFragment extends Fragment {
    private News currentNews;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_detail, container, false);

        TextView titleView = view.findViewById(R.id.news_detail_title);
        TextView authorView = view.findViewById(R.id.news_detail_author);
        TextView contentView = view.findViewById(R.id.news_detail_content);

        if (currentNews != null) {
            titleView.setText(currentNews.getTitle());
            authorView.setText("作者：" + currentNews.getAuthor());
            contentView.setText(currentNews.getContent());
        }

        return view;
    }

    public void setNews(News news) {
        this.currentNews = news;
        // 如果已经加载了 UI，则更新显示
        if (getView() != null) {
            TextView titleView = getView().findViewById(R.id.news_detail_title);
            TextView authorView = getView().findViewById(R.id.news_detail_author);
            TextView contentView = getView().findViewById(R.id.news_detail_content);

            if (currentNews != null) {
                titleView.setText(currentNews.getTitle());
                authorView.setText("作者：" + currentNews.getAuthor());
                contentView.setText(currentNews.getContent());
            }
        }
    }
}